﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Minecraft.Config.Csv.CsvCom;
using Minecraft.Model;

namespace Minecraft.Config.Csv.CsvCls
{
	/// <summary>
	/// 家具配置表
	/// </summary>
	public partial class Mod_Drawing
	{

		/// <summary>
		/// 家具最大等级
		/// </summary>
		public int MaxLevel_Convert
		{
			get
			{
				return int.Parse(Level);
			}
		}
		/// <summary>
		/// 图纸类型int
		/// </summary>
		public int Type_Convert
		{
			get
			{
				return int.Parse(Type);
			}
		}
		/// <summary>
		/// 家具类型枚举
		/// </summary>
		public EnumFurnitureType enumFurnitureType_Convert
		{
			get
			{
				int.TryParse(Type, out int TypeInt);
				return (EnumFurnitureType)TypeInt;
			}
		}
		/// <summary>
		/// 耐久/生命
		/// </summary>
		public int Hp_Convert
		{
			get
			{
				return Convert.ToInt32(Hp);
			}
		}
		/// <summary>
		/// 耐久自动恢复速度（每分钟n个）
		/// </summary>
		public int Hp_speed_Convert
		{
			get
			{
				return int.Parse(Hp_speed);
			}
		}

		/// <summary>
		/// 耐久值衰减
		/// </summary>
		public int Hp_expend_Convert
		{
			get
			{
				return int.Parse(Hp_expend);
			}
		}

		/// <summary>
		/// 家具在基地中可以放置的数量上限（0表示没有上限，大于0表示有上限）
		/// </summary>
		public int Max_count_Convert
		{
			get
			{
				return int.Parse(Max_count);
			}
		}
		/// <summary>
		/// （针对机关家具）造成的伤害
		/// </summary>
		public int Att_Convert
		{
			get
			{
				return int.Parse(Att);
			}
		}
		/// <summary>
		/// （针对存储家具）获取存储家具可以存储的goodsItemId列表
		/// </summary>
		/// <returns></returns>
		public List<string> GetCanStorageGoodsItemIdList()
		{
			var typeEnums = Store_type.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries)
				.ToList().ConvertAll(m => (EnumGoodsType)int.Parse(m));
			if (typeEnums == null || !typeEnums.Any())
			{
				throw new Exception("存储家具的Store_type字段为空，请联系策划");
			}
			List<string> itemList = new List<string>();
			if (typeEnums.Count() == 1 && typeEnums.FirstOrDefault() == EnumGoodsType.All)
			{
				var enumGoodsTypeAllList = EnumGoodsType.All.GetEnumDesxriptionDict().Select(k => (EnumGoodsType)k.Key).ToList();
				enumGoodsTypeAllList.Remove(EnumGoodsType.All);
				//itemList = CsvConfig.itemsList.Where(m => enumGoodsTypeAllList.Any(k => k == m.enumGoodsType_Convert)).Select(m => m.Id).ToList();
				itemList = CsvConfig.GetitemsDict().Where(m => enumGoodsTypeAllList.Any(k => k == m.Value.enumGoodsType_Convert)).Select(m => m.Value.Id).ToList();
			}
			else
			{
				if (typeEnums.Any(m => m == EnumGoodsType.All))
				{
					throw new Exception("存储家具的Store_type字段配置出错（既包含all，又包含其他类型），请联系策划");
				}
				//itemList = CsvConfig.itemsList.Where(m => typeEnums.Any(k => k == m.enumGoodsType_Convert)).Select(m => m.Id).ToList();
				itemList = CsvConfig.GetitemsDict().Where(m => typeEnums.Any(k => k == m.Value.enumGoodsType_Convert)).Select(m => m.Value.Id).ToList();
			}
			return itemList;
		}
		/// <summary>
		/// （针对存储家具）存储家具的容纳个数
		/// </summary>
		public int Store_Convert
		{
			get
			{
				int StoreInt = int.Parse(Store);
				if (StoreInt <= 0)
				{
					throw new Exception("存储家具的容纳个数 配置出错，请联系策划");
				}
				return StoreInt;
			}
		}

		/// <summary>
		/// （针对产出家具）得到指定等级下的家具产出效率（例如：100|120|140，则产出效率分别为100%、120%、140%，取浮点数：1.00、1.20、1.40）
		/// </summary>
		/// <param name="level"></param>
		/// <returns></returns>
		public double GetEfficiency_Convert(int theLevel)
		{
			if (theLevel < 1 || theLevel > MaxLevel_Convert)
			{
				throw new Exception($"传入的家具等级参数错误【{theLevel}】");
			}
			if (Efficiency != "-1")
			{
				return (int.Parse(Efficiency.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries)[theLevel - 1])) * 1.0 / 100;
			}
			else
			{
				return 1;
			}
		}
		/// <summary>
		/// （针对产出家具）是否需要燃料
		/// </summary>
		public bool If_buring_Convert
		{
			get
			{
				if (If_buring == "1")
				{
					return true;
				}
				else
				{
					return false;
				}
			}
		}
		/// <summary>
		/// （针对产出家具，制作）产出家具指定等级下的产出配方列表信息列表
		/// </summary>
		public List<ModRecipeTimeInfo> GetOutputModRecipeTimeInfoList_Convert(int theCurLevel)
		{
			List<ModRecipeTimeInfo> modRecipeTimeInfoList = new List<ModRecipeTimeInfo>();
			for (int level = 1; level <= theCurLevel; level++)
			{
				modRecipeTimeInfoList.AddRange(GetModRecipeTimeInfoList(level, Recipe_time));
			}
			return modRecipeTimeInfoList;
		}

		/// <summary>
		/// （针对产出家具，升级）产出家具指定等级下的升级配方列表信息列表
		/// </summary>
		/// <param name="theCurLevel"></param>
		/// <returns></returns>
		public ModRecipeTimeInfo GetUpgradeModRecipeTimeInfoList_Convert(int theCurLevel)
		{
			return GetModRecipeTimeInfoList(theCurLevel, Up_materials).FirstOrDefault();
		}
		/// <summary>
		/// （针对产出家具，修理）产出家具指定等级下的修理配方列表信息列表
		/// </summary>
		/// <param name="theCurLevel"></param>
		/// <returns></returns>
		public ModRecipeTimeInfo GetRepairModRecipeTimeInfoList_Convert(int theCurLevel)
		{
			//if (theCurLevel < 1 || theCurLevel > MaxLevel_Convert - 1)
			//{
			//	throw new Exception($"传入的家具等级参数错误【{theCurLevel}】");
			//}
			return GetModRecipeTimeInfoList(theCurLevel, Hp_materials).FirstOrDefault();
		}

		/// <summary>
		/// 解析配方数据
		/// </summary>
		/// <param name="fieldVal"></param>
		/// <returns></returns>
		private static List<ModRecipeTimeInfo> GetModRecipeTimeInfoList(int theCurLevel, string fieldVal)
		{
			if (fieldVal.Contains("#"))
			{
				List<ModRecipeFurnitureInfo> modFurnitureRecipeInfoList = new List<ModRecipeFurnitureInfo>();
				//按照等级拆分
				var levelStrs = fieldVal.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

				for (int level = 1; level <= levelStrs.Length; level++)
				{
					var levelStr = levelStrs[level - 1];
					//同一等级下的多个配方方案
					var recipes = levelStr.Split(new char[] { '+' }, StringSplitOptions.RemoveEmptyEntries);
					List<ModRecipeTimeInfo> modRecipeTimeInfoList = new List<ModRecipeTimeInfo>();
					foreach (var recipe in recipes)
					{
						//拆分配方id和时间
						var recipeTimes = recipe.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);
						ModRecipeTimeInfo modRecipeTimeInfo = new ModRecipeTimeInfo
						{
							RecipeId = recipeTimes[0],
							Second = int.Parse(recipeTimes[1]) * 60
						};
						modRecipeTimeInfoList.Add(modRecipeTimeInfo);
					}
					ModRecipeFurnitureInfo modRecipeFurnitureInfo = new ModRecipeFurnitureInfo
					{
						Level = level,
						modRecipeTimeInfoList = modRecipeTimeInfoList
					};
					modFurnitureRecipeInfoList.Add(modRecipeFurnitureInfo);
				}
				return modFurnitureRecipeInfoList.FirstOrDefault(m => m.Level == theCurLevel).modRecipeTimeInfoList;
			}
			else
			{
				return new List<ModRecipeTimeInfo>();
			}
		}


		/// <summary>
		/// （针对产出家具，制作）根据获取该产出家具指定等级之前的所有制作配方列表信息（依赖配方表）
		/// </summary>
		public List<Mod_Compositing> GetCompositingList_Convert(int theCurLevel)
		{
			if (theCurLevel < 1 || theCurLevel > MaxLevel_Convert)
			{
				throw new Exception($"传入的家具等级参数错误【{theCurLevel}】");
			}
			var modRecipeTimeInfoList = GetOutputModRecipeTimeInfoList_Convert(theCurLevel);
			//return CsvConfig.compositingList.Where(c => modRecipeTimeInfoList.Any(m => m.RecipeId == c.Id)).ToList();
			return CsvConfig.GetcompositingDict().Where(c => modRecipeTimeInfoList.Any(m => m.RecipeId == c.Value.Id)).Select(m => m.Value).ToList();
		}

		/// <summary>
		/// （针对产出家具，升级）根据获取该产出家具指定等级下的升级配方信息（依赖配方表）(问了策划，每个阶段的升级配方只可能有一种)
		/// </summary>
		public Mod_Compositing GetUpgradeCompositingList_Convert(int theCurLevel)
		{
			var modRecipeTimeInfo = GetUpgradeModRecipeTimeInfoList_Convert(theCurLevel);
			if (modRecipeTimeInfo == null)
			{
				throw new Exception($"该等级【{theCurLevel}】下的升级配方为空，请联系策划");
			}
			return CsvConfig.GetcompositingDict().FirstOrDefault(c => modRecipeTimeInfo.RecipeId == c.Value.Id).Value;
		}

		/// <summary>
		/// （针对产出家具，修理）根据获取该产出家具指定等级下的修理配方信息（依赖配方表）(问了策划，每个阶段的修理配方只可能有一种)
		/// </summary>
		public Mod_Compositing GetRepairCompositingList_Convert(int theCurLevel)
		{
			//if (theCurLevel < 1 || theCurLevel > MaxLevel_Convert - 1)
			//{
			//	throw new Exception($"传入的家具等级参数错误【{theCurLevel}】");
			//}
			var modRecipeTimeInfo = GetRepairModRecipeTimeInfoList_Convert(theCurLevel);
			//return CsvConfig.compositingList.FirstOrDefault(c => modRecipeTimeInfo.RecipeId == c.Id);
			return CsvConfig.GetcompositingDict().FirstOrDefault(c => modRecipeTimeInfo.RecipeId == c.Value.Id).Value;
		}

		/// <summary>
		/// （针对产出家具）根据获取产出家具指定等级下的所需格子总个数（所需材料种类格子（以最多种类合成配放为基准） + 一个燃料格子（如果可以燃烧，就加上）+ 一个产出格子）（按照这个顺序对Position排序，从0开始）
		/// </summary>
		public int GetCellTotalNum_Convert(int level)
		{
			if (level < 1 || level > MaxLevel_Convert)
			{
				throw new Exception($"传入的家具等级参数错误【{level}】");
			}
			var totalNum = 0; //家具需要摆放的格子总数
			List<Mod_Compositing> mod_CompositingList = GetCompositingList_Convert(level);
			var maxRecipeNum = mod_CompositingList.Max(m => m.modRecipeInfoList_Convert.Count());
			totalNum += maxRecipeNum; //加上材料摆放的格子
			if (If_buring_Convert)
			{
				totalNum++; //如果是否需要燃料，就加上一个存放燃料的格子
			}
			totalNum++; //加上放置产物的格子
			return totalNum;
		}
	}
}
